<?php


namespace app\common\model\user;


use app\common\model\BaseModel;
use think\model\concern\SoftDelete;


/**
 * openvpn控制器模型
 * Class Openvpn
 * @package app\common\model\user
 */
class Openvpn extends BaseModel
{
    use SoftDelete;
    protected $name = 'openvpn';
    protected $deleteTime = 'delete_time';

    // 在模型中定义一个静态方法进行状态更新
    public static function updateStatus()
    {
        try {
            // 合并更新操作，减少数据库交互次数
            $ips = self::column('vpn_ip');
            foreach ($ips as $ip) {
                $status = pingAddress($ip);
                echo $ip.' '.$status.PHP_EOL;
                self::update(['status' => $status], ['vpn_ip' => $ip]);
            }
        } catch (DbException $e) {
            app()->log->record($e->getMessage(), 'ERROR');
        }
    }
}